home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / nurbsTrim / nurbsTrim.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  7KB  |  230 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *  Clicking any mouse button toggles variable useTrim; when True, the
  19.  *  nurbs surface is trimmed with the nurbs curve; when False, trim is
  20.  *  not done.  On my workstation, an Indigo 2 Extreme running Irix 5.2,
  21.  *  attempting to trim the nurbs curve causes it do disappear altogether.
  22.  *  This suggests that bug #210400, "NURBS Trim Curve Bug", resides in
  23.  *  OpenGL, not in OpenInventor.
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <GL/glx.h>
  29. #include <GL/gl.h>
  30. #include <GL/glu.h>
  31.  
  32. static int sglBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, None};
  33. static int dblBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
  34.  
  35. Display *    dpy;
  36. Window          win;
  37. GLboolean    doubleBuffer = GL_TRUE;
  38. GLUnurbsObj *    NurbsSrf;
  39.  
  40.  
  41. void
  42. fatalError( char *message )
  43. {
  44.     fprintf(stderr, "NurbsProfile: %s\n", message);
  45.     exit(1);
  46. }
  47.  
  48.  
  49. void
  50. redraw( GLboolean useTrim )
  51. {
  52.     const        GLint    uSize    = 10;
  53.     const        GLint    vSize    = 9;
  54.     const    static    GLfloat    uKnotVector [10] =
  55.         { 0., 1., 2., 3., 4., 5., 6., 7., 8., 9. };
  56.     const    static    GLfloat    vKnotVector [9] = 
  57.         { 2., 2., 2., 2., 3., 4., 4., 4., 4. };
  58.     const        GLint    uStride    = 15;
  59.     const        GLint    vStride    = 3;
  60.  
  61.     const    static    GLfloat    sCP [6][5][3] = {
  62. { {4.0, 1.,4.0}, {4.0, 1.,4.0}, {4.0, 1.,4.0}, {4.0, 1.,4.0}, {4.0, 1.,4.0} },
  63. { {4.0, 1.,4.0}, {3.5, 1.,3.1}, {5.0, 1.,4.0}, {3.5, 1.,4.9}, {3.5, 1.,3.1} },
  64. { {5.0, 1.,4.0}, {3.5, 1.,4.9}, {2.4, 0.,1.3}, {7.5, 0.,4.0}, {2.4, 0.,6.7} },
  65. { {2.4, 0.,1.3}, {7.2, 0.,4.0}, {2.4, 0.,6.7}, {3.5,-1.,3.1}, {5.0,-1.,4.0} },
  66. { {3.5,-1.,4.9}, {3.5,-1.,3.1}, {5.0,-1.,4.0}, {3.5,-1.,4.9}, {4.0,-1.,4.0} },
  67. { {4.0,-1.,4.0}, {4.0,-1.,4.0}, {4.0,-1.,4.0}, {4.0,-1.,4.0}, {4.0,-1.,4.0} }
  68.     };
  69.     const        GLint    uOrder    = 4;
  70.     const        GLint    vOrder    = 4;
  71.  
  72.     const        GLint    cSize    = 9;
  73.     const    static    GLfloat    cKnotVector [9] =
  74.         { 0., 0., 1., 2., 3., 4., 5., 6., 6. };
  75.     const        GLint    cStride = 2;
  76.  
  77.     const    static    GLfloat    cCP [7][2] = {
  78.         {5.00, 3.83},    /* a */
  79.         {4.82, 3.78},    /* b */
  80.         {4.25, 3.64},    /* c */
  81.         {4.11, 3.26},    /* d */
  82.         {5.00, 3.16},    /* e */
  83.         {4.81, 3.52},    /* f */
  84.         {5.00, 3.83}    /* a */
  85.     };
  86.     const        GLint    cOrder    = 2;
  87.  
  88.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  89.     gluBeginSurface( NurbsSrf );
  90.     gluNurbsSurface( NurbsSrf, uSize, (GLfloat *)uKnotVector,
  91.         vSize, (GLfloat *)vKnotVector, uStride, vStride,
  92.         (GLfloat *)sCP, uOrder, vOrder, GL_MAP2_VERTEX_3 );
  93.     if ( useTrim )
  94.     {
  95.         gluBeginTrim( NurbsSrf );
  96.         gluNurbsCurve(   NurbsSrf, cSize, (GLfloat *)cKnotVector,
  97.             cStride, (GLfloat *)cCP, cOrder, GLU_MAP1_TRIM_2 );
  98.         /*
  99.         gluPwlCurve(   NurbsSrf, cSize-2, (GLfloat *)cCP,
  100.             cStride, GLU_MAP1_TRIM_2 );
  101.          */
  102.         gluEndTrim( NurbsSrf );
  103.     }
  104.     gluEndSurface( NurbsSrf );
  105.  
  106.     if ( doubleBuffer )
  107.         glXSwapBuffers( dpy, win );    /* implicit glFlush */
  108.     else
  109.         glFlush( );    /* explicit flush for single buffered case */
  110. }
  111.  
  112.  
  113. void
  114. main( int argc, char ** argv )
  115. {
  116.     XVisualInfo *        vi;
  117.     Colormap        cmap;
  118.     XSetWindowAttributes    swa;
  119.     GLXContext        cx;
  120.     XEvent            event;
  121.     GLboolean        needRedraw    = GL_FALSE;
  122.     GLboolean        recalcModelView    = GL_TRUE;
  123.     GLfloat            distance    = 8.;
  124.     GLboolean        useTrim        = GL_FALSE;
  125.     GLfloat            xAngle        =  12.0;
  126.     GLfloat            yAngle        =  82.0;
  127.     GLfloat            zAngle        = 112.0;
  128.     int            dummy;
  129.  
  130.     dpy = XOpenDisplay(NULL);
  131.     if (dpy == NULL)
  132.         fatalError("could not open display");
  133.     if(!glXQueryExtension(dpy, &dummy, &dummy))
  134.         fatalError("X server has no OpenGL GLX extension");
  135.  
  136.     /* find an OpenGL-capable RGB visual with depth buffer */
  137.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), dblBuf);
  138.     if (vi == NULL)
  139.     {
  140.         vi = glXChooseVisual(dpy, DefaultScreen(dpy), sglBuf);
  141.         if (vi == NULL)
  142.             fatalError("no RGB visual with depth buffer");
  143.         doubleBuffer = GL_FALSE;
  144.     }
  145.     if(vi->class != TrueColor)
  146.         fatalError("TrueColor visual required for this program");
  147.  
  148.     /* create an OpenGL rendering context */
  149.     cx = glXCreateContext( dpy, vi, None, GL_TRUE );
  150.     if (cx == NULL)
  151.         fatalError("could not create rendering context");
  152.  
  153.     /* create an X colormap since probably not using default visual */
  154.     cmap = XCreateColormap( dpy, RootWindow(dpy, vi->screen),
  155.         vi->visual, AllocNone );
  156.     swa.colormap = cmap;
  157.     swa.border_pixel = 0;
  158.     swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
  159.     win = XCreateWindow( dpy, RootWindow(dpy, vi->screen), 0, 0,
  160.         300, 300, 0, vi->depth, InputOutput, vi->visual,
  161.         CWBorderPixel | CWColormap | CWEventMask, &swa );
  162.     XSetStandardProperties( dpy, win, "NurbsProfile", "NurbsProfile",
  163.         None, argv, argc, NULL);
  164.  
  165.     /* bind the rendering context to the window */
  166.     glXMakeCurrent( dpy, win, cx );
  167.  
  168.     XMapWindow( dpy, win );
  169.  
  170.     /* configure the OpenGL context for rendering */
  171.     glEnable( GL_DEPTH_TEST );    /* enable depth buffering */
  172.     glEnable( GL_AUTO_NORMAL );    /*   & automatic normal calculation */
  173.     glClearColor(0.1, 0.1, 0.3, 0.0);
  174.  
  175.     /* set up projection transform */
  176.     glMatrixMode(GL_PROJECTION);
  177.     glLoadIdentity();
  178.     glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
  179.  
  180.     /* create NURBS object */
  181.     NurbsSrf = gluNewNurbsRenderer( );
  182.     gluNurbsProperty( NurbsSrf, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON );
  183.  
  184.     /* dispatch X events */
  185.     while (1)
  186.     {
  187.         do
  188.         {
  189.             XNextEvent( dpy, &event );
  190.             switch ( event.type )
  191.             {
  192.             case ButtonPress:
  193.                 recalcModelView = GL_TRUE;
  194.                 useTrim = useTrim ? GL_FALSE : GL_TRUE;
  195.                 break;
  196.             case ConfigureNotify:
  197.                 glViewport(0, 0, event.xconfigure.width,
  198.                     event.xconfigure.height);
  199.                 needRedraw = GL_TRUE;
  200.                 break;
  201.             case Expose:
  202.                 needRedraw = GL_TRUE;
  203.                 break;
  204.             }
  205.         }
  206.         while(XPending(dpy)); /* loop to compress events */
  207.  
  208.         if (recalcModelView)
  209.         {
  210.             glMatrixMode( GL_MODELVIEW );
  211.             glLoadIdentity(); /* set modelview to identity matrix */
  212.             /* move the camera to where you can see something */
  213.             glTranslatef( 0.0, 0.0, -distance );
  214.             /* rotate by X, Y, and Z angles */
  215.             glRotatef( xAngle, 0.1, 0.0, 0.0 );
  216.             glRotatef( yAngle, 0.0, 0.1, 0.0 );
  217.             glRotatef( zAngle, 0.0, 0.0, 1.0 );
  218.             recalcModelView = GL_FALSE;
  219.             needRedraw      = GL_TRUE;
  220.         }
  221.  
  222.         if (needRedraw)
  223.         {
  224.             redraw( useTrim );
  225.             needRedraw = GL_FALSE;
  226.         }
  227.     }
  228. }
  229.  
  230.